package monkeyXML;

import java.util.ArrayList;
import java.util.Iterator;

public class XMLTag extends XMLNode
{
   private String theTag;
   private ArrayList<Attrib> theAttributes;

   public XMLTag( String aTagPlusAtts )
   {
//System.out.println( aTagPlusAtts );
      int index = 1;
      boolean inQuotes = false;
      
      char current = aTagPlusAtts.charAt( index );
      
      StringBuffer tag = new StringBuffer();
      while( current != '>' && current != '/' && current != ' ' )
      {
         tag.append( current );
         current = aTagPlusAtts.charAt( ++index );
      }
      
      theTag = tag.toString();
//System.out.println( theTag );
      
      if( current != '>' && current != '/' )
      {
         do{
            current = aTagPlusAtts.charAt( ++index );
         }while( current == ' ' && current == '\n' );
      }
      
      if( current != '>' && current != '/' )
      {
         theAttributes = new ArrayList<Attrib>();
      }
      
      while( current != '>' && current != '/' )
      {
         Attrib attrib = new Attrib();
         
         StringBuffer temp = new StringBuffer();
         
         while( current == ' ' || current == '\n' )
         {
            current = aTagPlusAtts.charAt( ++index );
         }
         
         while( current != '=' && current != ' ' )
         {
            temp.append( current );
            current = aTagPlusAtts.charAt( ++index );
         }         
//System.out.print( "new attrib " + temp.toString() );
         attrib.label = temp.toString();
         temp.delete( 0, temp.length() );
         
         while( current != '=' )
         {
            current = aTagPlusAtts.charAt( ++index );
         }
         
         do{
            current = aTagPlusAtts.charAt( ++index );
         }while( current == ' ' || current == '\n' );
         
         while( current != ' ' && current != '\n' && current != '/' 
               && current != '>' || inQuotes )
         {
            if( current == '"' ) 
            { 
               inQuotes = !inQuotes; 
            }
            else
            {
               temp.append( current );
            }
            current = aTagPlusAtts.charAt( ++index );
         }
         attrib.value = temp.toString();
//System.out.println( " = " + attrib.value + '!' );         
         theAttributes.add( attrib );
      }
   }
   
   @Override
   public String toString()
   {
      StringBuffer theFullTag = new StringBuffer();
      theFullTag.append( theTag );
      if( theAttributes != null )
      {
         Iterator<Attrib> i = theAttributes.iterator();
         Attrib a = null;
         do{
            a = i.next();
            theFullTag.append( " " + a.label + "=" + a.value );
         }while( i.hasNext() );
      }
      return theFullTag.toString();
   }
   
   public String getTag()
   {
      return theTag;
   }
   
   public String readAttrib( String anAttrib )
   {
      boolean found = false;
      Iterator<Attrib> i = theAttributes.iterator();
      String result = null;
      while( i.hasNext() && !found )
      {
        Attrib a = i.next();
         if( a.label.equalsIgnoreCase( anAttrib ) )
         {
            result = a.value;
         }
      }
      return result;
   }
   
   private class Attrib
   {
      public String label;
      public String value;
   }
}
